package cn.itcast.eduorder.service.impl;

import cn.itcast.commonutils.ordervo.CourseWebVoOrder;
import cn.itcast.commonutils.ordervo.UCenterMemberOrder;
import cn.itcast.eduorder.client.EduClient;
import cn.itcast.eduorder.client.UCenterClient;
import cn.itcast.eduorder.entity.Order;
import cn.itcast.eduorder.mapper.OrderMapper;
import cn.itcast.eduorder.service.OrderService;
import cn.itcast.eduorder.utils.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Duanzihuang
 * @since 2020-06-20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private EduClient eduClient;

    @Autowired
    private UCenterClient uCenterClient;

    @Override
    public String createOrder(String courseId, String memberId) {
        // 获取课程信息
        CourseWebVoOrder courseInfo = eduClient.getCourseInfo(courseId);

        // 获取用户信息
        UCenterMemberOrder memberInfo = uCenterClient.getMemberInfo(memberId);

        // 创建order
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseInfo.getId());
        order.setCourseCover(courseInfo.getCover());
        order.setCourseTitle(courseInfo.getTitle());
        order.setTotalFee(courseInfo.getPrice());

        order.setTeacherName(courseInfo.getTeacherName());
        order.setMemberId(memberInfo.getId());
        order.setMobile(memberInfo.getMobile());
        order.setNickname(memberInfo.getNickname());

        order.setPayType(1);
        order.setStatus(0);

        // 保存订单
        baseMapper.insert(order);

        // 返回订单编号
        return order.getOrderNo();
    }
}
